import { ICircle, IVec2 } from "../../util/Defined";
import { TypeUtil } from "../../util/TypeUtil";
import { Vec2 } from "./Vec2";

/**
 * 圆形
 */
export class Circle extends Vec2 implements ICircle {
	/** 零点 */
	static readonly ZERO = TypeUtil.readonlyType(new Circle(0, 0, 0));
	/** 临时变量 */
	static readonly T = new Circle();
	/** 临时变量 */
	static readonly T2 = new Circle();
	/** 临时变量 */
	static readonly T3 = new Circle();
	/** 临时变量 */
	static readonly T4 = new Circle();
	/** 临时变量 */
	static readonly T5 = new Circle();

	constructor(x = 0, y = 0, public radius = 0) {
		super(x, y);
	}

	/**
	 * 获取圆外一点到圆的切点
	 * @param p 圆外一点
	 */
	getTangentPoint(p: Readonly<IVec2>) {
		const dis = this.dis(p);
		const radCenter = Vec2.radTo(p, this);
		const spread = Math.acos(this.radius / dis);
		const rad1 = radCenter + spread;
		const rad2 = radCenter - spread;
		const p1 = this.copyPos().moveRad(rad1, dis);
		const p2 = this.copyPos().moveRad(rad2, dis);
		return { p1, p2 };
	}
}
